<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/29 11:11
 * Modified Time: 2019/6/29 11:11
 * Version: 2.0
 */

namespace App\Modules\Activity\Services;


use App\Exceptions\AllValidatorException;
use App\Exceptions\NotFoundException;
use App\Lib\Auth\FLAuth;
use App\Lib\Utilities;
use App\Modules\Activity\Criterias\ActivityCriteria;
use App\Modules\Activity\Criterias\ActivityDetailsCriteria;
use App\Modules\Activity\Repositories\ActivityRepository;
use App\Modules\System\Repositories\DictionaryItemRepository;
use App\Modules\System\Repositories\UserStoreRepository;
use Flower\Core\Repository\Eloquent\BaseService;

class ActivityService extends BaseService
{

    private $FLAuth;
    private $activityRepository;
    private $dictionaryItemRepository;
    private $userStoreRepository;

    public function __construct(ActivityRepository $activityRepository,
                                UserStoreRepository $userStoreRepository,
                                DictionaryItemRepository $dictionaryItemRepository,
                                FLAuth $FLAuth)
    {
        $this->FLAuth = $FLAuth;
        $this->activityRepository = $activityRepository;
        $this->dictionaryItemRepository = $dictionaryItemRepository;
        $this->userStoreRepository = $userStoreRepository;
    }

    /**
     * 发布店铺活动
     * @param $store_id
     * @param $title
     * @param $playbill
     * @param $content
     * @param $expire
     * @return mixed
     */
    public function createActivity($store_id, $title, $playbill, $content, $expire)
    {
        $expire = strtotime($expire."23:59:59");
        $activity = $this->activityRepository->createActivity($store_id, $title, $playbill, $content, $expire);

        return $activity;

    }

    /**
     * 编辑店铺活动
     * @param $activity_id
     * @param $title
     * @param $playbill
     * @param $content
     * @return mixed
     */
    public function editActivity($activity_id, $title, $playbill, $content)
    {
        //更新数据
        return $this->activityRepository->editActivity($activity_id, $title, $playbill, $content);
    }

    /**
     * 删除店铺活动公告
     * @param $activity_id
     * @return int
     */
    public function delActivity($activity_id)
    {
        return $this->activityRepository->delActivity($activity_id);
    }

    /**
     * 获取店铺活动列表
     * @param $store_id
     * @param $pageSize
     * @return array|mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getActivityListPage($store_id,$pageSize)
    {
        $activity_list = $this->activityRepository->pushCriteria(new ActivityCriteria($store_id))->paginate($pageSize, ['*'], 'paginate');

        //处理分页数据
        $activity_list = Utilities::dealPage($activity_list);
        return $activity_list;
    }

    /**
     * 小程序端获取公告列表
     * @param $store_id
     * @param $pageSize
     * @return array|mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getActivityListPageWx($store_id,$pageSize)
    {
        $activity_list = $this->activityRepository->pushCriteria(new ActivityCriteria($store_id,1))->paginate($pageSize, ['*'], 'paginate');

        //处理分页数据
        $activity_list = Utilities::dealPage($activity_list);
        return $activity_list;
    }

    /**
     * 获取公告详情
     * @param $activity_id
     * @param $store_id
     * @throws NotFoundException
     */
    public function getActivity($activity_id)
    {
        $activity = $this->activityRepository->pushCriteria(new ActivityDetailsCriteria($activity_id))->first();
        if (!$activity) {
            throw new AllValidatorException(['msg' => '公告不存在', 'statusCode' => 400, 'errorCode' => 1404]);
        }
        return $activity;
    }

    /**
     * 获取活动海报
     * @param Request $request
     */
    public function getActivityPlaybill()
    {
        return $this->dictionaryItemRepository->getActivityPlaybill();
    }

}